package top.mschen.blog.common.config.auth2;

import com.alibaba.fastjson.JSONObject;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * 用来解决认证过的用户访问无权限资源时的异常
 */
public class AccessDeniedHandlerExt  implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        Map map = new  HashMap();
        map.put("success",false);
        map.put("resultCode",9999);
        map.put("resultMessage","没有访问权限!");
        response.getWriter().print(JSONObject.toJSONString(map));
    }
}
